#include "gd32f30x.h"
#include "timer.h"
/*!
    \brief      pwm output timer
    \param[in]  
    \param[out] 
    \retval
 */
void Timer0Config(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
    timer_break_parameter_struct timer_breakpara;

    rcu_periph_clock_enable(RCU_TIMER0);

    timer_deinit(TIMER0);

    /* TIMER0 configuration */
    timer_initpara.prescaler         = 0;                               /* 120MHz/(0+1) = 120MHz 计数周期 */
    timer_initpara.alignedmode       = TIMER_COUNTER_CENTER_BOTH;       /* 中央对齐计数，下溢产生中断 */
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;                /*  */
    timer_initpara.period            = 2999;                            /* 120MHz/(2*(2999+1)) = 20kHz  载波周期 */
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;                
    timer_initpara.repetitioncounter = 1;
    timer_init(TIMER0,&timer_initpara);

    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
	timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_HIGH;
    timer_ocintpara.ocnidlestate = TIMER_OC_IDLE_STATE_LOW;
	
    timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
    timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
    timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
	timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
    
	timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
	timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM1);
	
    timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
	timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
	timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
	timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);

    timer_breakpara.runoffstate      = TIMER_ROS_STATE_DISABLE;
    timer_breakpara.ideloffstate     = TIMER_IOS_STATE_DISABLE;
    timer_breakpara.deadtime         = 36;								  /* 300ns  死区时间 */
    timer_breakpara.breakpolarity    = TIMER_BREAK_POLARITY_LOW;
    timer_breakpara.outputautostate  = TIMER_OUTAUTO_ENABLE;
    timer_breakpara.protectmode      = TIMER_CCHP_PROT_0;
    timer_breakpara.breakstate       = TIMER_BREAK_DISABLE;      		  /* 失能刹车 */
	timer_break_config(TIMER0, &timer_breakpara);

    timer_primary_output_config(TIMER0,ENABLE);

    timer_auto_reload_shadow_enable(ENABLE);
	
	timer_interrupt_enable(TIMER0, TIMER_INT_UP);
	timer_enable(TIMER0);
	
	timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 2998);
}

/*!
    \brief      adc trigger timer
    \param[in]  
    \param[out] 
    \retval
 */

void Timer1Config()
{
	timer_parameter_struct timer_initpara;
	timer_oc_parameter_struct timer_ocintpara;
	
	rcu_periph_clock_enable(RCU_TIMER1);
	
	timer_deinit(TIMER1);
    
	/* TIMER1 configuration */
    timer_initpara.prescaler         = 11999;                               /* 120MHz/(0+1) = 120MHz 计数周期 */
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;              /* 边沿对齐计数 */
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;                /* 上升计数 */
    timer_initpara.period            = 9999;                            /* 120MHz/(5999+1) = 20kHz  与载波周期一致 */
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);
	
	timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
	timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
	
	timer_interrupt_enable(TIMER1, TIMER_INT_UP);
	timer_enable(TIMER1);
}
void TimerConfig(void)
{
	nvic_irq_enable(TIMER0_UP_IRQn,1U, 0U);                       /* 载波中断 */
    nvic_irq_enable(TIMER1_IRQn,4U, 0U);
	
	Timer1Config();
	Timer0Config();
	
}
